#include <learn/check_gl.h>
#include <learn/geom/vec.h>

GLfloat mixed[] = {-0.5, -0.5, 0.0, 1, 0, 0, 0.5, 0.0, 0.0, 0, 1, 0, 0.0, 0.5, 0.0, 0, 0, 1};

void init()
{
    // 注意这两步不能合并
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), &mixed[0]);
    glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), &mixed[3]);
}

void render()
{
    // 通过索引图元绘制
    glBegin(GL_TRIANGLES);
    glArrayElement(0);
    glArrayElement(1);
    glArrayElement(2);
    glEnd();

    // 直接绘制
    int ind[] = {0, 1, 2};
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, ind);
}

int main()
{
    if (!glfwInit())
    {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    GLFWwindow *window = glfwCreateWindow(800, 600, "Three Stars", nullptr, nullptr);
    if (!window)
    {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    init();
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();

    return 0;
}
